/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package itc.com.co.beans.utils;

import EntidadesCM.AfiliadoCM;
import java.util.List;
import javax.faces.model.ListDataModel;
import org.primefaces.model.SelectableDataModel;

public class AfiliadoDataModel extends ListDataModel<AfiliadoCM> implements SelectableDataModel<AfiliadoCM> {

    private List<AfiliadoCM> cars = null;

    public AfiliadoDataModel() {
    }

    public AfiliadoDataModel(List<AfiliadoCM> data) {
        super(data);
        if (data != null && !data.isEmpty()) {
            cars = data;
        } else {
            cars = (List<AfiliadoCM>) getWrappedData();
        }

    }

    @Override
    public AfiliadoCM getRowData(String rowKey) {
        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  

        try {
            if (cars == null || cars.isEmpty()) {
                cars = (List<AfiliadoCM>) getWrappedData();
            }

            for (AfiliadoCM car : cars) {
                if (car.getIdAfiliado().equals(Integer.valueOf(rowKey))) {
                    return car;
                }
            }

        } catch (NumberFormatException e) {
        }
        return null;
    }

    @Override
    public Object getRowKey(AfiliadoCM car) {
        return car.getIdAfiliado();
    }

    public void setCars(List<AfiliadoCM> cars) {
        this.cars = cars;
    }
}
